//path是nodejs基本包,用来处理路径
//__dirname目录地址,也就是根目录
//output输出路径
//解析vue-loader,需要添加VueLoaderPlugin
const VueLoaderPlugin = require('vue-loader/lib/plugin');
var HtmlPlugin = require('html-webpack-plugin');
var path = require('path');
const { webpack } = require('webpack');

var isDev = process.env.NODE_ENV === 'development'
const config = {
    target: 'web',
    entry: path.join(__dirname, 'src/index.js'),
    output: {
        filename: 'bundle.js',
        path: path.join(__dirname, 'dist')
    },
    module: {
        rules: [{
                //声明.vue结尾的文件使用vue-loader进行解析
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            //声明.css结尾的文件使用css-loader进行解析
            {
                test: /\.css$/i,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.jsx$/,
                loader: 'babel-loader'
            },
            //url-loader依赖于file-loader
            {
                test: /\.(gif|jpg|jpeg|png|svg)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 1024,
                        name: '[name].[ext]'
                    }
                }]
            },
            {
                //css预处理器
                // test: /\.styl$/,
                test: /\.styl(us)?$/,
                use: [
                    'style-loader',
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            sourceMap: true
                        }
                    },
                    'stylus-loader'
                ]
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new HtmlPlugin()
        //process.env.NODE_ENV
        // new webpack.DefinePlugin({
        //     'process.env': {
        //         NODE_ENC: isDev ? '"development"' : '"production"'
        //     }
        // })
    ]
}

if (isDev) {
    config.devtool = '#cheap-module-eval-soure-map'
    config.devServer = {
        port: 8080,
        host: '0.0.0.0',
        //webpack发生错误都显示在网页上
        overlay: {
            errors: true
        },
        historyFallback: {

        },
        //不会重新加载整个页面数据,部分页面渲染
        hot: true
            //open:true
    }
    config.plugins.push(
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin()
    )
}

module.exports = config